/**
 * 对象深拷贝
 * @param {Object} obj
 */
export const clone = (obj) => {
    return JSON.parse(JSON.stringify(obj));
}
export const clear = (obj) => {
    Object.keys(obj).map(key => {
        delete obj[key]
    })
}
/**
 * 对象属性赋值
 * @param obj 被复制对象
 * @param target 赋值对象
 */
export const copeProperties = (obj, target) => {
    if (obj && target) {
        Object.keys(obj).forEach((key) => {
            target[key] = obj[key];
        })
    }
}
export const convertToStringifiedJson = (str) => {
    // 替换单引号为双引号（如果字符串值本应该包含单引号，这会导致问题）
    let jsonStr = str.replace(/'/g, '"');
    // 确保整个对象用双引号包围
    jsonStr = `{${jsonStr}}`;
    // 移除花括号（如果它们是因为错误地包含在原始字符串中）
    jsonStr = jsonStr.replace(/^{/, '').replace(/}$/, '').replace(/(\w+):(\d+)/g, '"$1":$2');
    // 尝试解析JSON字符串
    let obj = JSON.parse(jsonStr);
    for (let key in obj) {
        if (typeof obj[key] === 'number') {
            obj[key] = obj[key].toString();
        }
    }
    return obj;
}
/**
 * 获取两个对象差异属性
 * @param o1 被对照对象
 * @param o2 目标对象
 */
const getDifferenceProperties = (o1, o2) => {
    let differenceProperties = {};
    if (o1 && o2) {
        Object.keys(o1).forEach((k) => {
            if (o2[k] !== undefined && o2[k] != o1[k]) {
                differenceProperties[k] = o2[k];
            }
        })
    }
    return differenceProperties;
}
export const fillTemplate = (template, params) => {
    return template.replace(/\$\{(\w+)\}/g, (_, key) => params[key] || '');
}
export const setArrayObjectKeyValue = (array, key, value, prop) => {
    let props = prop.split(",");
    for (let o of array) {
        if (props.indexOf(o.prop) != -1 || props.indexOf(o.field) != -1) {
            o[key] = value;
        }
    }
}

export const getFirstLetter = (str) => {
    let unicode = str.charCodeAt(0);
    if (unicode >= 19968 && unicode <= 40869) {
        let index = parseInt((unicode - 19968) / 408) + 1;
        return String.fromCharCode(64 + index);
    } else {
        return str.charAt(0);
    }
}
export default {
    clone, clear, copeProperties, getDifferenceProperties, getFirstLetter, convertToStringifiedJson
}
